package 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.geom.Point;
	
	[SWF(backgroundColor='#AAAAFF')]
	
	public class Main extends Sprite
	{
		[Embed(source="../res/background.png")]
        public var Background:Class;
		public var background:Bitmap = new Background(); //We use two backgrounds in order to have infinite scrolling
		public var background2:Bitmap = new Background();
		
		public var boat:Boat;
		
		public var startTime:Number;
		public var framerate:int = 60;
		
		public var cleaner:Function = function():void{};
		
		public var items:Array = new Array(	{"title": "Jouer",		"onClick": inGame },
											{"title": "A propos",	"onClick": inAbout },
											{"title": "Nos bateaux","onClick": inBoats } );
		
		public var menu:Menu;
		public var about:About;
		
		public var tf:TextField;
		
		function Main()
		{
			this.background.height = stage.stageHeight; //setting the backgrounds to stageHeight
			this.background2.height = stage.stageHeight;
			
			menu = new Menu(stage, this.items);
			about = new About(stage, inMenu);
			
			inMenu(new Event("empty"));
		}
		
		public function inMenu(e:Event):void { //Creates the menu and adds event listeners
			cleaner();
			stage.addChild(menu);
			cleaner = outMenu;
		}
		
		public function outMenu():void { //Removes the menu, its event listeners and launches the next state
			stage.removeChild(menu);
		}
		
		public function inGame(e:Event):void{ //Start gamestate.
			cleaner();
			cleaner = outGame;
			
			this.background.x = 0; //Set the position of the backgrounds 
			this.background2.x = this.background.width;
			
			stage.addChild(this.background);
			stage.addChild(this.background2);
			
			this.boat = new Boat();
			var enemy:Intelligence = new Intelligence(new Boat(), new Point(200, 200));
			stage.addChild(enemy);
			stage.addChild(boat);
			
			stage.addEventListener(Event.ENTER_FRAME, boat.move);
			stage.addEventListener(Event.ENTER_FRAME, enemy.move);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, boat.keyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, boat.keyRelease);
		}
		
		public function outGame():void {
			stage.removeChild(this.background);
			stage.removeChild(this.background2);
			stage.removeChild(boat);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, boat.keyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, boat.keyRelease);
			
		}
		
		public function inAbout(e:Event):void{ //Start gamestate.
			cleaner();
			cleaner = outAbout;
			
			stage.addChild(about);
		}
		
		public function outAbout():void {
			stage.removeChild(about);
		}
		
		public function inPause(e:Event):void {
			cleaner();
			cleaner = outPause;
			
			stage.removeChild(about);
		}
		
		public function outPause():void{
			
		}
		
		public function inBoats():void{
			cleaner();
			cleaner = outBoats;
			
			
		}
		
		public function outBoats():void{
			
		}
		
		public function scrollBackground():void{
			if(this.background.x + this.background.width < 0){// Make the background loop
				this.background.x = this.background2.x+this.background2.width;
			} else if(this.background2.x + this.background2.width < 0){
				this.background2.x = this.background.x+this.background.width;
			}
			this.background.x -= 0.5;
			this.background2.x -= 0.5;
		}
		
		public function close(e:Event):void{
			clearStage(new Event("empty"));
		}
		
		public function clearStage(e:Event):void { //removing all the children
			while(stage.numChildren != 0){ stage.removeChildAt(0) };
		}
		
		public function isWin():void
		{
			if(boat.imageBoat.x > stage.stageWidth)
			{
				this.tf = new TextField();
				
				this.tf.text = "VICTOIRE !";
				this.tf.x = stage.stageWidth/2;
				this.tf.y = stage.stageHeight/2;
				this.tf.textColor = 0xFFFFFF;
				stage.addChild(this.tf);
			}
		}
		
		public function refresh(e:Event):void
		{
			boat.move();
			this.isWin();
		}
	}
}